#include "HelloWorldScene.h"

USING_NS_CC;
#define TEETH_TAG 100
#define MOUSE_TAG 50
Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();

    scene->addChild(layer);
    return scene;
}

void HelloWorld::openMouse()
{
    _openFlag = true;
    this->clearTeenth();
    auto mouseLabel = dynamic_cast<Label*>(this->getChildByTag(MOUSE_TAG));
    mouseLabel->setString("张开");
    for(int i = 0; i < 20; i++){
        auto tooth = this->getChildByTag(TEETH_TAG + i);
        if(i < 10){
            tooth->setPosition(tooth->getPosition() + Vec2(0, -30));
        }else{
            tooth->setPosition(tooth->getPosition() + Vec2(0, 30));
        }
    }
}

void HelloWorld::closeMouse()
{
    _openFlag = false;
    this->clearTeenth();
    auto mouseLabel = dynamic_cast<Label*>(this->getChildByTag(MOUSE_TAG));
    mouseLabel->setString("闭合");
    for(int i = 0; i < 20; i++){
        auto tooth = this->getChildByTag(TEETH_TAG + i);
        if(i < 10){
            tooth->setPosition(tooth->getPosition() + Vec2(0, 30));
        }else{
            tooth->setPosition(tooth->getPosition() + Vec2(0, -30));
        }
    }
}

void HelloWorld::warning(const std::string str)
{
    auto warnLabel = Label::createWithTTF(str, "fonts/yaya.ttf", 30);
    warnLabel->setColor(Color3B::BLACK);
    this->addChild(warnLabel,1);
    warnLabel->setPosition(Vec2(_visibleSize.width / 2, 580));
    auto moveBy = MoveBy::create(1.0f, Vec2(0, 50));
    auto fadeout = FadeOut::create(1.0f);
    warnLabel->runAction(Sequence::create(
        Spawn::create(moveBy, fadeout, NULL),
        DelayTime::create(1.0f),
        RemoveSelf::create(),
        NULL));
}

void HelloWorld::initButton()
{
    //Title
    auto title = Label::createWithTTF("智能牙刷Demo", "fonts/yaya.ttf", 35);
    title->setColor(Color3B::BLACK);
    title->setAnchorPoint(Vec2(0, 0.5));
    title->setPosition(Vec2(45, _visibleSize.height - 50));
    this->addChild(title, 1);
    
    //控制4个方向信号
    auto frontLabel = Label::createWithTTF("正", "fonts/yaya.ttf", 30);
    frontLabel->setColor(Color3B::BLACK);
    auto backLabel = Label::createWithTTF("反", "fonts/yaya.ttf", 30);
    backLabel->setColor(Color3B::BLACK);
    auto upLabel = Label::createWithTTF("上", "fonts/yaya.ttf", 30);
    upLabel->setColor(Color3B::BLACK);
    auto downLabel = Label::createWithTTF("下", "fonts/yaya.ttf", 30);
    downLabel->setColor(Color3B::BLACK);
    
    //控制震动信号开关
    auto shockLabel = Label::createWithTTF("震动: ", "fonts/yaya.ttf", 30);
    shockLabel->setColor(Color3B::BLACK);
    shockLabel->setPosition(Vec2(_visibleSize.width - 100, _visibleSize.height - 50));
    this->addChild(shockLabel,1);
    auto shockSwitch = MenuItemLabel::create(Label::createWithTTF("开", "fonts/yaya.ttf", 30), [=](Ref* pRef){
        if(!_shockflag){
            _shockflag = true;
            log("震动已打开");
            dynamic_cast<MenuItemLabel*>(pRef)->setString("关");
        }
        else{
            _shockflag = false;
            log("震动已关闭");
            dynamic_cast<MenuItemLabel*>(pRef)->setString("开");
        }
    });
    shockSwitch->setColor(Color3B::BLACK);
    shockSwitch->setPosition(Vec2(_visibleSize.width - 50, _visibleSize.height - 50));
    
    //牙齿开合
    auto toothLabel = Label::createWithTTF("牙齿: ", "fonts/yaya.ttf", 30);
    toothLabel->setColor(Color3B::BLACK);
    toothLabel->setPosition(Vec2(200, 250));
    this->addChild(toothLabel,1);
    
    auto mouseLabel = Label::createWithTTF("闭合", "fonts/yaya.ttf", 30);
    mouseLabel->setColor(Color3B::BLACK);
    mouseLabel->setTag(MOUSE_TAG);
    mouseLabel->setPosition(Vec2(280 , 250));
    this->addChild(mouseLabel, 1);
    
    auto frontButton = MenuItemLabel::create(frontLabel, [=](Ref* pRef){
        if (_openFlag) {
            this->closeMouse();
        }
        BrushTeeth(BrushMode::Front);
    });
    frontButton->setPosition(Vec2(_visibleSize.width / 5, 100));
    auto backButton = MenuItemLabel::create(backLabel, [=](Ref* pRef){
        if (_openFlag) {
            this->closeMouse();
        }
        BrushTeeth(BrushMode::Back);
    });
    backButton->setPosition(Vec2(_visibleSize.width / 5 * 2, 100));
    auto upButton = MenuItemLabel::create(upLabel, [=](Ref* pRef){
        if(!_openFlag){
            this->openMouse();
        }
        BrushTeeth(BrushMode::Up);
    });
    upButton->setPosition(Vec2(_visibleSize.width / 5 * 3, 100));
    auto downButton = MenuItemLabel::create(downLabel, [=](Ref* pRef){
        if(!_openFlag){
            this->openMouse();
        }
        BrushTeeth(BrushMode::Down);
    });
    downButton->setPosition(Vec2(_visibleSize.width / 5 * 4, 100));
    auto menu = Menu::create(frontButton, backButton, upButton, downButton, shockSwitch,  nullptr);
    menu->setAnchorPoint(Vec2(0.5, 0.5));
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
}

void HelloWorld::initToothRegion()
{
    for(auto i = 0; i < 2; i++){
        for(auto j = 0; j < 10; j++){
            auto tooth = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("white.png"));
            this->addChild(tooth,1);
            tooth->setTag(TEETH_TAG + i * 10 + j);
            tooth->setPosition(Vec2(60 + 40 * j, 420 + 40 * i));
            //初始化teeth数组
            Tooth t;
            t.isBrushing = false;
            t.isClear = false;
            t.num = 0;
            teeth[i*10 + j] = t;
        }
    }
}

void HelloWorld::clearTeenth()
{
    for(auto i = 0; i < 20; i++){
        if (!teeth[i].isBrushing) {
            continue;
        }
        auto tooth = dynamic_cast<Sprite*>(this->getChildByTag(TEETH_TAG + i));
        tooth->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("white.png"));
    }
}

void HelloWorld::BrushTeeth(BrushMode mode)
{
    switch (mode) {
        case BrushMode::Front:
        {
            log("牙刷正着拿");
            clearTeenth();
            for(auto i = 0; i < 2; i++){
                for(auto j = 0; j < 6; j++){
                    auto tooth = dynamic_cast<Sprite*>(this->getChildByTag(TEETH_TAG + i * 10 + j));
                    tooth->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("yellow.png"));
                    teeth[i * 10 + j].isBrushing = true;
                }
            }
            break;
        }
        case BrushMode::Back:
        {
            log("牙刷反着拿");
            clearTeenth();
            for(auto i = 0; i < 2; i++){
                for(auto j = 6; j < 10; j++){
                    auto tooth = dynamic_cast<Sprite*>(this->getChildByTag(TEETH_TAG + i * 10 + j));
                    tooth->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("yellow.png"));
                    teeth[i * 10 + j].isBrushing = true;
                }
            }
            break;
        }
        case BrushMode::Up:
        {
            log("牙刷朝上");
            clearTeenth();
            for(auto i = 10; i < 20; i++){
                auto tooth = dynamic_cast<Sprite*>(this->getChildByTag(TEETH_TAG + i));
                tooth->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("yellow.png"));
                teeth[i].isBrushing = true;
            }
            break;
        }
        case BrushMode::Down:
        {
            log("牙刷朝下");
            clearTeenth();
            for(auto i = 0; i < 10; i++){
                auto tooth = dynamic_cast<Sprite*>(this->getChildByTag(TEETH_TAG + i));
                tooth->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("yellow.png"));
                teeth[i].isBrushing = true;
            }
            break;
        }
        default:
            break;
    }
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto colorLayer = LayerColor::create(Color4B::WHITE);
    this->addChild(colorLayer, 0);
    
    _visibleSize = Director::getInstance()->getVisibleSize();
    
    this->initButton();
    this->initToothRegion();
    
    return true;
}